import axios from 'axios'
import querystring from 'querystring'
import Host from '../../host.config'

const ax = axios.create({
    baseURL: Host.apiHost,
    timeout: 10000
})

// 拦截器
ax.interceptors.request.use(config => {
    //         config.headers.post = { 'Content-Type': 'application/json;charset=UTF-8' }
    if (config.method === 'put') {
        if (config.url.indexOf('/uploadMovieVideo') !== -1) {
            config.headers.put = {'Content-Type': 'multipart/form-data'}
        } else {
            config.data = querystring.stringify(config.data)
        }
    } else if (config.method === 'post') {
        config.headers.post = { 'Content-Type': 'application/x-www-form-urlencoded' }
        config.data = querystring.stringify(config.data)
    }
    // }
    return config
}, error => {
    return Promise.reject(error)
})

ax.interceptors.response.use(response => {
    return response.data
}, error => {
    console.log('axios.response: ============================', { Error: error.Error, config: error.config })
    return {
        returnCode: 1200,
        msg: 'error',
        data: null
    }
})

export default ax
